package site.xuxingcan.tvlauncher;

import android.content.Context;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

/**
 * Created by xuxingcan on 2017/1/9.
 */
public class HttpUtil {
    public static void getImageURI(Context mContext) throws Exception {
        File file = new File(mContext.getDir("pic", Context.MODE_PRIVATE), "startup.jpg");
        URL url = new URL("https://s3.cn-north-1.amazonaws.com.cn/emcdata/application/androidtv/homepage/pic/startup.jpg");
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        if (conn.getResponseCode() == 200) {
            InputStream is = conn.getInputStream();
            FileOutputStream fos = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            is.close();
            fos.close();
        }
    }
}
